package com.umbr3lla.main.state;

import java.io.IOException;
import java.util.Collections;
import java.util.Vector;

import com.umbr3lla.main.model.Logger;
import com.umbr3lla.main.state.GamePreferences.NET_TYPE;
import com.umbr3lla.main.state.cards.PlayerCard;
import com.umbr3lla.main.state.cards.ResourceCard;
import com.umbr3lla.main.state.cards.sets.base.characters.ChrisRedfield;
import com.umbr3lla.main.state.cards.sets.base.characters.ChrisRedfieldPromo;
import com.umbr3lla.network.INetworkLayer;
import com.umbr3lla.network.socket.ClientSocketHandler;


public class GameState 
{
	//-------------------------------------- static
	private static GameState mainGameState = new GameState();
	
	public static GameState getGameState()
	{
		return mainGameState;
	}
	//------------------------------------------------------
	
	private INetworkLayer playerNetLayer;
	private Vector<PlayerCard> allChars = new Vector<PlayerCard>();
	
	//---------------------------------- game
	
	private boolean gameEnded = false;
	private Vector<Player> players = 
		new Vector<Player>();
	
	private Vector<Vector<ResourceCard>> resourcePiles = 
			new Vector<Vector<ResourceCard>>();
	
	private Player currentPlayer=null;
	
	//---------------------------------------------
	
	
	public GameState()
	{
		setupPlayerLayer();
		setupResources();
	}
	
	private void setupPlayerLayer()
	{
		if(GamePreferences.netType == NET_TYPE.SOCKET)
		{
			try {
				playerNetLayer = new ClientSocketHandler();
			} catch (IOException e) {
				Logger.writeToErrorLog(this, e.toString());
				System.exit(-1);
			}
			
			playerNetLayer.startLayer();
			
		}
		else if(GamePreferences.netType == NET_TYPE.NONE)
		{
			//TODO dynamic class loading
		}
	}
	
	
	private void setupResources() {
		//TODO
	
		//add all the available chars
		allChars.add(new ChrisRedfield());
		allChars.add(new ChrisRedfieldPromo());
		
	}

	public INetworkLayer getPlayerIFaceLayer()
	{
		return playerNetLayer;
	}
	
	
	//-----------------------------------------------------
	
	public PlayerCard getRandomPlayerCard()
	{
		Collections.shuffle(allChars);
		return allChars.get(0);
	}
	
	
	public Player getCurrentPlayer()
	{
		return currentPlayer;
	}
	
	public void addPlayer(Player p)
	{
		if(gameEnded)
			players.add(p);
	}
	
	public void removePlayer(Player p)
	{
		if(!gameEnded)
		{
			endCurrentGame();
		}
	}
	
	
	//-------------------------------------------------
	private void initPlayerSetup() {
		//TODO
	}
	
	public void handlePlayersTurn()
	{
		while(!gameEnded)
		{
			for(Player p: players)
			{
				currentPlayer = p;
				p.resetTurn();
				p.startTurn();
				p.endTurn();
			}
		
		}
	}
	
	//---------------------------------------------
	
	public void startNewGame()
	{
		//check to see if at least one player is playing
		if(players.size()  >0)
			currentPlayer = players.get(0);
		else{
			Logger.writeToErrorLog(this, "Not enough Players to start game");
			gameEnded = true;
			return;
		}
		
		gameEnded = false;
		
		initPlayerSetup();
		handlePlayersTurn();
	}

	public void endCurrentGame()
	{
		gameEnded = true;
	}
	
	
	
}
